
export default {
  install (Vue, options) {
    let store = options.store
    // debugger
    Vue.directive('permission', {
      inserted: function (el, binding, vnode) {
        let checkCodes = []
        let access = true
        if (binding.arg === 'role') {
          checkCodes = store.state.router.btns
        }
        for (let need of binding.value) {
          if (!checkCodes.includes(need)) {
            access = false
            break
          }
        }
        if (!access) {
          el.parentNode.removeChild(el)
        }
      }
    })
    Vue.prototype.hasPermissions = (permissions) => {
      // let isAdmin = store.state.d2admin.isAdmin
      // if (isAdmin) {
      //   return true
      // }
      let has = false
      // let checkCodes = store.state.d2admin.functions.concat(store.state.d2admin.roles)
      let checkCodes = store.state.router.btns
      for (let need of permissions) {
        if (checkCodes.some(s => s === need)) {
          has = true
          break
        }
      }
      return has
    }
    // Vue.prototype.hasFunctions = (functions) => {
    //   let isAdmin = store.state.d2admin.permission.isAdmin
    //   if (isAdmin) {
    //     return true
    //   }
    //   let has = false
    //   let checkCodes = store.state.d2admin.permission.functions
    //   for (let need of functions) {
    //     if (checkCodes.some(s => s === need)) {
    //       has = true
    //       break
    //     }
    //   }
    //   return has
    // }
    // Vue.prototype.hasRoles = (roles) => {
    //   let isAdmin = store.state.d2admin.permission.isAdmin
    //   console.log(isAdmin, 'isAdmin')
    //   if (isAdmin) {
    //     return true
    //   }
    //   let has = false
    //   let checkCodes = store.state.d2admin.permission.roles
    //   for (let need of roles) {
    //     if (checkCodes.some(s => s === need)) {
    //       has = true
    //       break
    //     }
    //   }
    //   return has
    // }
  }
}
